<template>
  <Top-Page class="search-box">
    <!-- <cu-custom bgColor="bg-gradual-green" :isBack="true">
			<template #backText></template>
			<template #content>{{ title }}</template>
		</cu-custom> -->
    <!-- :style="[{top:CustomBar + 'px'}]" -->
    <view class="cu-bar search-wrap">
      <view class="search-left" @tap="BackPage">
        <text class="cuIcon-back"></text>
      </view>
      <view class="search-form round">
        <text class="cuIcon-search"></text>
        <input type="text" v-model="keywords" placeholder="输入搜索的关键词" confirm-type="search" />
      </view>
      <view class="search-right">
        <view v-show="!keywords" @tap="BackPage">取消</view>
        <view v-show="keywords" @tap="search">搜索</view>
      </view>
    </view>

    <view style="margin-top: 120rpx">
      <view v-if="type < 3 && contacts.length > 0">
        <view class="padding">联系人</view>
        <view class="cu-list menu-avatar no-padding">
          <view class="cu-item" v-for="(items, sub) in contacts" :key="sub" @tap="openDetails(items)">
            <view class="cu-avatar lg radius mr-15" :style="[{ backgroundImage: 'url(' + items.avatar + ')' }]"></view>
            <view class="content">
              <view class="c-333">{{ items.realname }}</view>
            </view>
          </view>
        </view>
      </view>
      <view v-if="list.length > 0">
        <view class="padding">{{ type === 4 ? "服务团队" : "群聊" }}</view>
        <view class="cu-list menu-avatar no-padding">
          <view class="cu-item" v-for="(items, sub) in list" :key="sub" @tap="openDetails(items)">
            <view class="cu-avatar lg radius mr-15" :style="[{ backgroundImage: 'url(' + items.avatar + ')' }]"></view>
            <view class="content">
              <view class="c-333">{{ items.realname }}</view>
            </view>
          </view>
        </view>
      </view>
      <view v-if="!list.length && !contacts.length">
        <Empty noDatatext="暂无数据" textcolor="#999"></Empty>
      </view>
    </view>
  </Top-Page>
</template>

<script>
/**
 * 初始的引导页
 */
import { useMsgStore } from "@/store/message";
import pinia from "@/store/index";
const msgStore = useMsgStore(pinia);
export default {
  name: "search",
  data() {
    return {
      title: "搜索聊天",
      keywords: "",
      allContacts: [],
      contacts: [],
      list: [],
      type: 1,
    };
  },
  watch: {
    keywords(val) {
      this.search();
    },
  },
  onLoad(options) {
    this.type = options.type;
    if (this.type == 2) {
      this.title = "搜索联系人";
    } else if (this.type == 3) {
      this.title = "搜索群聊";
    } else if (this.type == 4) {
      this.title = "搜索服务团队";
    } else {
      this.title = "搜索聊天";
    }
  },
  mounted() {
    this.allContacts = uni.getStorageSync("allContacts");
  },
  methods: {
    BackPage() {
      const allroutes = getCurrentPages();
      const cureentRoute = allroutes[allroutes.length - 1].route;
      // 如果当前在聊天页面，自动返回到首页，避免层级过深
      if (cureentRoute == "pages/message/chat") {
        uni.reLaunch({
          url: "/pages/index/index",
        });
        return;
      } else if (allroutes.length < 2 && "undefined" !== typeof __wxConfig) {
        let url = "/" + __wxConfig.pages[0];
        return uni.redirectTo({ url });
      }
      uni.navigateBack({
        delta: 1,
      });
    },
    search() {
      if (!this.keywords) return;

      const arr = this.$util.searchObject(
        this.allContacts,
        ["realname", "name_py", "account", "searchName"],
        this.keywords
      );
      const contacts = [];
      const list = [];
      arr.forEach((item) => {
        //搜索到后显示之前隐藏的信息
        if (item.is_hide && item.is_hide == 1) {
          let status = false;
          if (!!item.searchName) {
            if (item.searchName.indexOf(this.keywords) != -1) {
              status = true;
            } else {
              return;
            }
          } else {
            status = true;
          }

          if (status) {
            item.is_hide = 0;
            msgStore.updateContacts({
              change_hide: true,
              id: item.id,
              is_hide: item.is_hide,
            });
          }
        }

        if (this.type == 4) {
          console.log(444);
          list.push(item);
        } else {
          if (item.is_group == 1) {
            list.push(item);
          } else {
            contacts.push(item);
          }
        }
      });
      this.list = list;
      this.contacts = contacts;
    },
    // 打开聊天
    openDetails(items) {
      if (this.type == 2 && items.is_gourp == 0) {
        uni.navigateTo({
          url: "/pages/contacts/detail?id=" + items.id,
        });
      }
      uni.navigateTo({
        url: "/pages/message/chat?id=" + items.id,
      });
    },
  },
};
</script>

<style scoped lang="scss">
.search-box {
  width: 750rpx;
  min-height: 100vh;
  background-color: #f2f2f2;

  .cu-bar {
    background-color: #f2f2f2;
  }
}
.search-wrap {
  position: fixed;
  width: 100%;
  // top: 0;
  top: var(--status-bar-height);
  z-index: 1024;
  min-height: auto;
  padding: 24rpx 32rpx;

  .search-left {
    font-size: 42rpx;
    color: #666666;
  }

  .search-right {
    font-size: 32rpx;
    color: #1a66ff;
  }
}

.cu-bar .search-form {
  background: #ffffff;
  margin: 0 16rpx;
  input {
    font-size: calc(var(--amplified_base) * 26rpx);
  }
}
</style>
